package com.fundamentalesandroid;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MultiTouchTest extends Activity implements OnTouchListener{

	StringBuilder builder=new StringBuilder();
	TextView textView;
	float[] x=new float[10];
	float[] y=new float[10];
	boolean[] tocado=new boolean[10];
	
	public void updateTextView(){
		this.builder.setLength(0);
		for(int i=0;i<10;i++){
			this.builder.append(tocado[i]);
			this.builder.append(",");
			this.builder.append(x[i]);
			this.builder.append(",");
			this.builder.append(y[i]);
			this.builder.append("\n");
		}
		this.textView.setText(builder.toString());
	
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		//obtengo la accion
		int action=event.getAction() & MotionEvent.ACTION_MASK;
		//obtengo el indice del dedo
		int pointerIndex=(event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) 
						>> MotionEvent.ACTION_POINTER_ID_SHIFT;
		//obtengo el id del dedo
		int pointerId=event.getPointerId(pointerIndex);
		this.builder.setLength(0);
		switch(action){
			case MotionEvent.ACTION_DOWN :
			case MotionEvent.ACTION_POINTER_DOWN :
				tocado[pointerId]=true;
				this.x[pointerId]=(int)event.getX(pointerIndex);
				this.y[pointerId]=(int)event.getY(pointerIndex);
				break;
			case MotionEvent.ACTION_UP :
			case MotionEvent.ACTION_POINTER_UP :
			case MotionEvent.ACTION_CANCEL :
				tocado[pointerId]=true;
				this.x[pointerId]=(int)event.getX(pointerIndex);
				this.y[pointerId]=(int)event.getY(pointerIndex);
				break;
			case MotionEvent.ACTION_MOVE :
				int	pointerCount=event.getPointerCount();
				for(int i=0;i<pointerCount;i++){
					pointerIndex=i;
					pointerId=event.getPointerId(pointerIndex);
					x[pointerId]=(int)event.getX(pointerIndex);
					y[pointerId]=(int)event.getY(pointerIndex);
				}
				
				break;
	
		}
		this.updateTextView();
		return true;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		textView=new TextView(this);
		textView.setText("Toca y arrastra ¡Soporta Multitouch!");
		textView.setOnTouchListener(this);
		this.setContentView(textView);
	}
		
	
}
